package com.guangli.ocr.ui

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import android.view.MotionEvent
import com.baidu.ocr.sdk.OCR
import com.baidu.ocr.sdk.OnResultListener
import com.baidu.ocr.sdk.exception.OCRError
import com.baidu.ocr.sdk.model.AccessToken
import com.blankj.utilcode.util.LogUtils
import com.guangli.base.base.activity.RobotBaseActivity
import com.guangli.base.util.ToastUtils
import com.guangli.ocr.R
import com.guangli.ocr.databinding.ActivityMainBinding
import com.guangli.ocr.viewmodel.MainViewModel
import com.yanzhenjie.permission.AndPermission

class MainActivity : RobotBaseActivity<ActivityMainBinding, MainViewModel>() {
    private val permissions = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA
    )
    override fun initComponents() {
        initView()
        initAccessTokenWithAkSk()

        getPermissions(permissions)
    }

    //动态请求权限
    fun getPermissions(permissions: Array<String>) {
        if (AndPermission.hasPermissions(this, permissions)) {

        } else {
            val stringArray = arrayOfNulls<String>(permissions.size)
            for (i in permissions.indices) {
                stringArray[i] = permissions[i]
            }
            AndPermission.with(this)
                .runtime()
                .permission(stringArray)
                .onGranted {

                }.onDenied {
                    ToastUtils.showShort("请允许同意权限")
                }.start();
        }
    }
    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_main

    override fun initVariableId(): Int = 1

    var hasGotToken = false
    /**
     * 用明文ak，sk初始化
     */
    private fun initAccessTokenWithAkSk() {
        OCR.getInstance(this).initAccessTokenWithAkSk(object : OnResultListener<AccessToken> {
            override fun onResult(result: AccessToken) {
                val token = result.accessToken
                hasGotToken = true
                viewModel.token.set(token)
                LogUtils.e(token)
            }

            override fun onError(error: OCRError) {
                error.printStackTrace()
                LogUtils.e("AK，SK方式获取token失败", error.message)
            } //}, getApplicationContext(),  "请填入您的AK", "请填入您的SK");
        }, applicationContext, "IoFaDTmr3ohk4rimXPzzCG8Q", "3QoyqLZ0tzG1cFQIurktLq5ZOS8LotcZ")
    }

    private fun initView(){
        binding.view1.setOnHoverListener { v, event ->
            LogUtils.e(event)
            val what = event.action
            when (what) {
                MotionEvent.ACTION_HOVER_ENTER -> {
                    LogUtils.e("bottom ACTION_HOVER_ENTER")
                    binding.view1.setBackgroundResource(R.mipmap.bg_select_f)
                }
                MotionEvent.ACTION_HOVER_MOVE -> LogUtils.e("bottom ACTION_HOVER_MOVE")
                MotionEvent.ACTION_HOVER_EXIT -> {
                    LogUtils.e("bottom ACTION_HOVER_EXIT")
                    binding.view1.setBackgroundColor(Color.TRANSPARENT)
                }
            }
            return@setOnHoverListener false
        }

        binding.view2.setOnHoverListener { v, event ->
            LogUtils.e(event)
            val what = event.action
            when (what) {
                MotionEvent.ACTION_HOVER_ENTER -> {
                    LogUtils.e("bottom ACTION_HOVER_ENTER")
                    binding.view2.setBackgroundResource(R.mipmap.bg_select_f)
                }
                MotionEvent.ACTION_HOVER_MOVE -> LogUtils.e("bottom ACTION_HOVER_MOVE")
                MotionEvent.ACTION_HOVER_EXIT -> {
                    LogUtils.e("bottom ACTION_HOVER_EXIT")
                    binding.view2.setBackgroundColor(Color.TRANSPARENT)
                }
            }
            return@setOnHoverListener false
        }

        binding.view3.setOnHoverListener { v, event ->
            LogUtils.e(event)
            val what = event.action
            when (what) {
                MotionEvent.ACTION_HOVER_ENTER -> {
                    LogUtils.e("bottom ACTION_HOVER_ENTER")
                    binding.view3.setBackgroundResource(R.mipmap.bg_select_f)
                }
                MotionEvent.ACTION_HOVER_MOVE -> LogUtils.e("bottom ACTION_HOVER_MOVE")
                MotionEvent.ACTION_HOVER_EXIT -> {
                    LogUtils.e("bottom ACTION_HOVER_EXIT")
                    binding.view3.setBackgroundColor(Color.TRANSPARENT)
                }
            }
            return@setOnHoverListener false
        }
    }
}